<?
class Biz_Team{
    //获得全部队伍数据
    function getAllTeam(){
        $cache_key	= "AllTeam";
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $team = new Data_Team();
            $res = $team->getAllTeam();
            if(count($res['result']) >0){
                $datas = $res['result'];
                foreach ($datas as $per){
                    $array[$per['id']] = $per;
                }
            }
            $datas = $array;
            Lib_Cache::set($cache_key,$datas);
        }
        return $datas;
    }
    //获得所有球队关键信息
    function getAllTeamList($season = null){
        $team_all_info = self::getAllTeam($season);
        foreach ($team_all_info as $per){
            $tmp['team_id'] = $per['team_id'];
            $tmp['team_name_s'] = self::name2short($per['team_name_s']);
            $tmp['team_name_l'] = $per['team_name_l'];
            $tmp['team_url'] = Biz_Common::getURL('team',$per['team_id'],$season);
            $team_all[] = $tmp;
            $tmp = array();
        }
        return $team_all_info = $team_all;
    }
    //获得全部队伍数据
    function getTeam($team_id,$season){
        $cache_key	= "ZC_Team_".$team_id."_".$season;
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $team = new Data_Team();
            $res = $team->getTeam($team_id,$season);
            if(count($res['result']) >0){
                $datas = $res['result'][0];
            }
            Lib_Cache::set($cache_key,$datas);
        }
        return $datas;
    }

    //通过赛季、ID信息获得球队信息,$type为0时,提取缩写,为1时,提取完整名称
    function teamid2name($season,$id,$type = 0){
        $gameinfo = self::getTeam($id,$season);
        switch ($type){
            case 0:return self::name2short($gameinfo['team_name_s']);break;
            case 1:return $gameinfo['team_name_s'];break;
            case 2:return $gameinfo['team_name_l'];break;
            default:break;
        }
    }
    //获取更简化名字
    function name2short($team_name){
        $cache_key	= "ZC_Team_sname_all";
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $team = new Data_Team();
            $res = $team->getAllTeamShort();
            if(count($res['result']) >0){
                foreach ($res['result'] as $per){
                    $datas[$per['team_name_l']] = $per['team_name_s'];
                }
            }
            Lib_Cache::set($cache_key,$datas);
        }
        if(isset($datas[$team_name])){
            return $datas[$team_name];
        }
        return $team_name;
    }
    //根据赛季和轮次获取积分榜
    function getRankBySeasonRound($season,$round){
        $cache_key	= "ZC_TeamRank_".$round."_".$season;
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $team = new Data_Team();
            $rank = $team->getRank($season,$round);
            if(count($rank['result'])>0){
                $rank = $rank['result'];
                foreach ($rank as $key=>$per){
                    $rank[$key]['team_url'] = Biz_Common::getURL('team',$per['team_id'],$season);
                    $rank[$key]['team_name_s'] = self::name2short($per['team_name_s']);
                }
                $datas = $rank;
            }
            Lib_Cache::set($cache_key,$datas);
        }
        return $datas;
    }
    //根据球队、赛季和轮次获取积分榜
    function getTeamRankInfo($season,$round,$team_id){
        $cache_key	= "ZC_TeamRankInfo_".$round."_".$season."_".$team_id;
        $datas		= Lib_Cache::get($cache_key);
        if(!$datas){
            $team = new Data_Team();
            $rank = $team->getTeamRankInfo($season,$round,$team_id);
            if(count($rank['result'])>0){
                $rank = $rank['result'][0];
                $datas = $rank;
            }
            Lib_Cache::set($cache_key,$datas);
        }
        return $datas;
    }
    /**
     * ###################################
     * 以下为crontab跑的时候用的函数 不使用缓存
     * ###################################
     * */

    //统计球队积分榜数据
    function buildTeamRank($condition){
        $team = new Data_Team();
        for( $season = $condition['begin_season'];$season <= $condition['end_season'];$season++){
            //上次排行
            $last_rank = array();
            $team_list = self::getAllTeam($season);
            //获取减分球队
            $descoreTeam = $team->getDescoreTeam($season);
            $team_rank = array();
            if(!empty($descoreTeam['result'])){
                foreach ($descoreTeam['result'] as $per){
                    $team_rank[$per['team_id']]['score'] -= $per['score'];
                }
            }
            //球队输赢记录列表 为了积分相同情况下排序
            $team_against = array();
            foreach ($team_list as $per){
                $team_rank[$per['team_id']]['team_id'] = $per['team_id'];
                $team_rank[$per['team_id']]['team_name_s'] = $per['team_name_s'];
                $team_rank[$per['team_id']]['team_name_l'] = $per['team_name_l'];
                $team_rank[$per['team_id']]['season'] = $per['season'];
                $team_against[$per['team_id']]['score'] = array();
            }
            if($season < $condition['begin_season'] || $season > $condition['end_season']){
                break;
            }
            for ($round = 1;$round <=30;$round++){
                if($round < $condition['begin_round'] || $round > $condition['max_round']){
                    break;
                }
                if($season == $condition['end_season'] && $round > $condition['end_round']){
                    break;
                }
                $res = self::getTeamStatsSeasonRound($season,$round);
                if($res)
                foreach ($res as $v){
                    $v_card_list = $team->getTeamStatsCard($season,$v['game_id'],$v['v_team_id']);
                    if($v_card_list['result']){
                        $team_rank[$v['v_team_id']]['yellow'] += $v_card_list['result'][0]['yellow'];
                        $team_rank[$v['v_team_id']]['red'] += $v_card_list['result'][0]['red'];
                    }
                    $h_card_list = $team->getTeamStatsCard($season,$v['game_id'],$v['h_team_id']);
                    if($h_card_list['result']){
                        $team_rank[$v['h_team_id']]['yellow'] += $h_card_list['result'][0]['yellow'];
                        $team_rank[$v['h_team_id']]['red'] += $h_card_list['result'][0]['red'];
                    }
                    if($v['h_total'] == $v['v_total']){
                        $v_score = 1;
                        $h_score = 1;
                        $team_rank[$v['v_team_id']]['draw'] ++;
                        $team_rank[$v['h_team_id']]['draw'] ++;
                        $team_against[$v['h_team_id']]['score'][$v['v_team_id']] ++;
                        $team_against[$v['v_team_id']]['score'][$v['h_team_id']] ++;
                    }elseif ($v['h_total'] > $v['v_total']){
                        $v_score = 0;
                        $h_score = 3;
                        $team_rank[$v['v_team_id']]['lose'] ++;
                        $team_rank[$v['h_team_id']]['win'] ++;
                        $team_against[$v['h_team_id']]['score'][$v['v_team_id']] += 3;
                        $team_against[$v['v_team_id']]['score'][$v['h_team_id']] += 0;
                    }elseif ($v['h_total'] < $v['v_total']){
                        $v_score = 3;
                        $h_score = 0;
                        $team_rank[$v['h_team_id']]['lose'] ++;
                        $team_rank[$v['v_team_id']]['win'] ++;
                        $team_against[$v['h_team_id']]['score'][$v['v_team_id']] += 0;
                        $team_against[$v['v_team_id']]['score'][$v['h_team_id']] += 3;
                    }
                    //积分
                    $team_rank[$v['h_team_id']]['round_count'] ++;
                    $team_rank[$v['v_team_id']]['round_count'] ++;;
                    //积分
                    $team_rank[$v['h_team_id']]['score'] += $h_score;
                    $team_rank[$v['v_team_id']]['score'] += $v_score;
                    //进失球数
                    $team_rank[$v['h_team_id']]['goal'] += $v['h_total'];
                    $team_rank[$v['h_team_id']]['be_goaled'] += $v['v_total'];
                    $team_rank[$v['v_team_id']]['goal'] += $v['v_total'];
                    $team_rank[$v['v_team_id']]['be_goaled'] += $v['h_total'];

                    $team_against[$v['h_team_id']]['goal'][$v['v_team_id']] += $v['h_total'];
                    $team_against[$v['h_team_id']]['be_goaled'][$v['v_team_id']] += $v['v_total'];
                    $team_against[$v['v_team_id']]['goal'][$v['h_team_id']] += $v['v_total'];
                    $team_against[$v['v_team_id']]['be_goaled'][$v['h_team_id']] += $v['h_total'];
                }
                self::teamRank($team_rank,$team_against);
//                if($round == 10) print_z($team_rank);
                foreach ($team_rank as $key=>$per){
                    unset($per['sum_score']);
                    unset($per['goal_more']);
                    unset($per['goal_more_all']);
                    unset($per['goal_vs']);
                    unset($per['card']);
                    $per['torank'] = isset($last_rank[$per['team_id']])?$last_rank[$per['team_id']]:0;
                    $last_rank[$per['team_id']] = $per['rank'];
                    $per['round'] = $round;
                    $per['updatetime'] = date("Y-m-d H:i:s");
                    //                    print_z($per);
                    $team->replaceRank($per);
                    $team_rank_tmp[$per['team_id']] = $per;
                    echo $season."赛季".$round."轮次".$per['team_name_l']." 排名已完成!\r\n";
                }
                $team_rank = $team_rank_tmp;
                //            echo "ID:".$per['reg_number']."=>".$per['name']." 完成!\r\n";
            }
        }
    }
    //获得某赛季某轮次Team的技术统计
    function getTeamStatsSeasonRound($season,$round){
        $team = new Data_Team();
        $res = $team->getTeamStatsSeasonRound($season,$round);
        if(count($res['result']) >0){
            $datas = $res['result'];
            return $datas;
        }
    }
    function teamRank(&$team_rank,$team_against){
        //先按积分排序
        self::arrayRank($team_rank,'score');
        self::checkSame($team_rank,$team_against,'score',1);
        foreach ($team_rank as $key=>$per){
            $team_rank[$key]['rank'] = $key+1;
        }
        //找相同积分的区块
    }
    function checkSame(&$team_rank,$team_against,$check_attr = 'score',$then_step = 1){
        $count = count($team_rank);
        foreach ($team_rank as $key=>$per){
            //积分相同的放入临时数组中
            if($key != $count-1 && $per[$check_attr] == $team_rank[$key+1][$check_attr]){
                $same_array[$key] = $per;
                $same_array[$key+1] = $team_rank[$key+1];
            }else{
                //临时数组有值
                if(!empty($same_array)){
                    $key_list = array_keys($same_array);
                    self::getStep($same_array,$team_against,$then_step);
                    foreach ($same_array as $k=>$v){
                        $team_rank[$key_list[$k]] = $v;
                    }
                }
                $same_array = array();
            }
        }
        return $team_rank;
    }
    function getStep(&$same_array,$team_against,$type = 1){
        switch ($type){
            case 1:return self::checkEveryScore($same_array,$team_against);break;
            case 2:return self::checkGoalMore($same_array,$team_against);break;
            case 3:return self::checkGoal($same_array,$team_against);break;
            case 4:return self::checkGoalMoreAll($same_array,$team_against);break;
            case 5:return self::checkGoalAll($same_array,$team_against);break;
            case 6:return self::checkCard($same_array,$team_against);break;
            default:break;
        }
    }
    //检查相互的积分
    function checkEveryScore(&$team_rank,$team_against){
        foreach ($team_rank as $key=>$val){
            $sum = 0;
            foreach ($team_rank as $per){
                if(isset($team_against[$val['team_id']]['score'][$per['team_id']])) $sum += $team_against[$val['team_id']]['score'][$per['team_id']];
            }
            $team_rank[$key]['sum_score'] = $sum;
        }
        self::arrayRank($team_rank,'sum_score');
        self::checkSame($team_rank,$team_against,'sum_score',2);
    }
    //检查相互净胜球
    function checkGoalMore(&$team_rank,$team_against){
        foreach ($team_rank as $key=>$val){
            $sum = 0;
            foreach ($team_rank as $per){
                if(isset($team_against[$val['team_id']]['goal'][$per['team_id']])) $sum += $team_against[$val['team_id']]['goal'][$per['team_id']] - $team_against[$val['team_id']]['be_goaled'][$per['team_id']];
            }
            $team_rank[$key]['goal_more'] = $sum;
        }
        self::arrayRank($team_rank,'goal_more');
        //        print_z($team_rank);
        self::checkSame($team_rank,$team_against,'goal_more',3);
    }
    //检查相互进球数
    function checkGoal(&$team_rank,$team_against){
        foreach ($team_rank as $key=>$val){
            $sum = 0;
            foreach ($team_rank as $per){
                if(isset($team_against[$val['team_id']]['goal'][$per['team_id']])) $sum += $team_against[$val['team_id']]['goal'][$per['team_id']];
            }
            $team_rank[$key]['goal_vs'] = $sum;
        }
        self::arrayRank($team_rank,'goal_vs');
        self::checkSame($team_rank,$team_against,'goal_vs',4);
    }
    //检查所有净胜球
    function checkGoalMoreAll(&$team_rank,$team_against){
        foreach ($team_rank as $key=>$per){
            $team_rank[$key]['goal_more_all'] = $per['goal'] - $per['be_goaled'];
        }
        self::arrayRank($team_rank,'goal_more_all');
        self::checkSame($team_rank,$team_against,'goal_more_all',5);
    }
    //检查所有进球
    function checkGoalAll(&$team_rank,$team_against){
        self::arrayRank($team_rank,'goal');
        self::checkSame($team_rank,$team_against,'goal',6);
    }
    //    检查所有红黄牌数
    function checkCard(&$team_rank,$team_against){
        foreach ($team_rank as $key=>$per){
            $team_rank[$key]['card'] = $per['yellow'] + $per['red'];
        }
        self::arrayRank($team_rank,'card');
        self::checkSame($team_rank,$team_against,'card',7);
    }
    //二维数组排序
    function arrayRank(&$team_rank,$value){
        sort($team_rank);
        //        print_z(count($team_rank));
        for ($i = 0;$i < count($team_rank);$i++){
            for ($j = 0;$j<count($team_rank)-$i;$j++){
                if($team_rank[$j][$value] < $team_rank[$j+1][$value]){
                    $tmp = $team_rank[$j];
                    $team_rank[$j] = $team_rank[$j+1];
                    $team_rank[$j+1] = $tmp;
                }
            }
        }
    }
    /**
     * #############################
     * CRONTAB的方法结束
     * #############################
     * */
}
?>